import React from 'react';
import type { Teacher, Classroom, Student, Schedule, Course } from './ScheduleConflictDetector';

interface SchedulingAlgorithmProps {
  teachers: Teacher[];
  classrooms: Classroom[];
  students: Student[];
  courses: Course[];
  currentSchedules: Schedule[];
  onGenerate: (newSchedules: Schedule[]) => void;
}

const SchedulingAlgorithm: React.FC<SchedulingAlgorithmProps> = ({
  teachers,
  classrooms,
  students,
  courses,
  currentSchedules,
  onGenerate
}) => {
  // 生成新的排课表
  const generateSchedule = () => {
    // 这里是简单的排课算法示例
    // 实际项目中需要根据更复杂的规则实现
    const newSchedules: Schedule[] = [];
    const existingScheduleIds = new Set(currentSchedules.map(s => s.id));
    
    // 遍历所有课程
    courses.forEach(course => {
      // 为每个课程分配教师、教室和时间
      const availableTeachers = teachers.filter(teacher => 
        isTeacherAvailable(teacher, course)
      );
      
      if (availableTeachers.length === 0) return;
      
      const teacher = availableTeachers[Math.floor(Math.random() * availableTeachers.length)];
      const classroom = classrooms[Math.floor(Math.random() * classrooms.length)];
      
      // 生成一个随机的开始时间
      const startDate = new Date();
      startDate.setDate(startDate.getDate() + Math.floor(Math.random() * 30));
      startDate.setHours(8 + Math.floor(Math.random() * 8)); // 8:00-16:00 之间
      startDate.setMinutes(0);
      
      // 结束时间设置为开始时间 + 2小时
      const endDate = new Date(startDate);
      endDate.setHours(startDate.getHours() + 2);
      
      const newSchedule: Schedule = {
        id: `schedule_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
        courseId: course.id,
        teacherId: teacher.id,
        classroomId: classroom.id,
        startTime: startDate.toISOString(),
        endTime: endDate.toISOString(),
        studentIds: students.slice(0, Math.min(30, students.length)).map(s => s.id)
      };
      
      // 检查是否与现有排课冲突
      if (!isScheduleConflict(newSchedule)) {
        newSchedules.push(newSchedule);
      }
    });
    
    onGenerate(newSchedules);
  };
  
  // 检查教师是否能教授该课程
  const isTeacherAvailable = (teacher: Teacher, course: Course): boolean => {
    // 实际项目中应该检查教师的专长、可用时间等
    return true;
  };
  
  // 检查排课是否与现有排课冲突
  const isScheduleConflict = (newSchedule: Schedule): boolean => {
    return currentSchedules.some(existing => {
      // 检查教师时间冲突
      if (existing.teacherId === newSchedule.teacherId) {
        if (isTimeOverlap(existing.startTime, existing.endTime, newSchedule.startTime, newSchedule.endTime)) {
          return true;
        }
      }
      
      // 检查教室时间冲突
      if (existing.classroomId === newSchedule.classroomId) {
        if (isTimeOverlap(existing.startTime, existing.endTime, newSchedule.startTime, newSchedule.endTime)) {
          return true;
        }
      }
      
      // 检查学生时间冲突
      const overlappingStudents = existing.studentIds.filter(id => newSchedule.studentIds.includes(id));
      if (overlappingStudents.length > 0) {
        if (isTimeOverlap(existing.startTime, existing.endTime, newSchedule.startTime, newSchedule.endTime)) {
          return true;
        }
      }
      
      return false;
    });
  };
  
  // 检查时间是否重叠
  const isTimeOverlap = (start1: string, end1: string, start2: string, end2: string): boolean => {
    const s1 = new Date(start1);
    const e1 = new Date(end1);
    const s2 = new Date(start2);
    const e2 = new Date(end2);
    
    return s1 < e2 && s2 < e1;
  };
  
  return (
    <div>
      <h3>智能排课算法</h3>
      <p>点击下方按钮生成新的排课方案</p>
      <div>
        <button onClick={generateSchedule}>生成排课方案</button>
      </div>
    </div>
  );
};

export default SchedulingAlgorithm;